home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / misc / edu / WhirlDisc.lha / WhirlDisc / Source / deflated_sequence.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-10  |  23.1 KB  |  851 lines

  1. /*
  2.  
  3. File: deflated_sequence.c
  4. Authors: Mark Adler, SDI & Neil Cafferkey
  5. Copyright (C) 2000 Neil Cafferkey
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  20. MA 02111-1307, USA.
  21.  
  22. */
  23.  
  24. #include "viewer.h"
  25. #include <exec/memory.h>
  26. #include <exec/execbase.h>
  27. #include <exec/libraries.h>
  28.  
  29. #include "deflated_sequence_protos.h"
  30. #include "general_protos.h"
  31. #include <proto/exec.h>
  32.  
  33.  
  34. /* If BMAX needs to be larger than 16, then h and x[] should be ULONG. */
  35. #define BMAX 16   /* maximum bit length of any code (16 for explode) */
  36. #define N_MAX 288 /* maximum number of codes in any set */
  37.  
  38. struct GZipData
  39. {
  40.   UBYTE *outbuf;
  41.   UBYTE *inbuf;
  42.   ULONG outsize;
  43.   ULONG insize;
  44.  
  45.   ULONG outpos;
  46.   ULONG inpos;
  47.   ULONG bb;
  48.   ULONG bk;
  49.  
  50.   ULONG data1[320]; /* all functions */
  51.  
  52.   /* huft_build */
  53.   ULONG c[BMAX+1];       /* bit length count table */
  54.   struct huft * u[BMAX]; /* table stack */
  55.   ULONG v[N_MAX];        /* values in order of bit length */
  56.   ULONG x[BMAX+1];       /* bit offsets, then code stack */
  57. };
  58.  
  59. #define BUFFEREXTEND 100
  60.  
  61.  
  62. /* Prototypes */
  63.  
  64. static BOOL decrunchZip(struct GZipData *);
  65. static LONG huft_build(ULONG *,ULONG,ULONG,UWORD *,UWORD *,struct huft **,
  66.    LONG *,struct GZipData *g);
  67. static VOID huft_free(struct huft *,struct GZipData *g);
  68. static LONG inflate_codes(struct huft *,struct huft *,LONG,LONG,
  69.    struct GZipData *g);
  70. static LONG inflate_stored(struct GZipData *g);
  71. static LONG inflate_fixed(struct GZipData *g);
  72. static LONG inflate_dynamic(struct GZipData *g);
  73.  
  74.  
  75.  
  76. /* Function: InflateSequence
  77.  * =========================
  78.  */
  79.  
  80. Sequence InflateSequence(DeflatedSequence deflated)
  81. {
  82.    Sequence inflated=NULL;
  83.    struct GZipData *g;
  84.    UBYTE *out_buffer;
  85.    BOOL success=TRUE;
  86.  
  87.    if((g=AllocMem(sizeof(struct GZipData),MEMF_CLEAR)))
  88.    {
  89.  
  90.       /* Get inflated length */
  91.  
  92.       g->inbuf=deflated->data;
  93.       g->insize=deflated->length;
  94.       g->outsize=0xffffffff;
  95.  
  96.       if(decrunchZip(g))
  97.       {
  98.  
  99.          /* Allocate the output buffer */
  100.  
  101.          if(out_buffer=AllocMem(g->outpos,MEMF_ANY))
  102.          {
  103.             if(inflated=CreateSequence(g->outpos,out_buffer))
  104.             {
  105.  
  106.                /* Inflate the data */
  107.  
  108.                g->inpos=0;
  109.                g->outpos=0;
  110.                g->bb=0;
  111.                g->bk=0;
  112.  
  113.                g->outbuf = inflated->data;
  114.  
  115.                if(!decrunchZip(g))
  116.                {
  117.                   ReportError(NULL,ERROR_REPORT_MEM,NULL,0);
  118.                   success=FALSE;
  119.                }
  120.             }
  121.             else
  122.             {
  123.                success=FALSE;
  124.             }
  125.  
  126.          }
  127.          else
  128.          {
  129.             ReportError(NULL,ERROR_REPORT_MEM,NULL,0);
  130.             success=FALSE;
  131.          }
  132.  
  133.          FreeMem(g,sizeof(struct GZipData));
  134.       }
  135.       else
  136.       {
  137.          ReportError(NULL,ERROR_REPORT_MEM,NULL,0);
  138.          success=FALSE;
  139.       }
  140.    }
  141.    else
  142.    {
  143.       ReportError(NULL,ERROR_REPORT_MEM,NULL,0);
  144.       success=FALSE;
  145.    }
  146.  
  147.    if(!success)
  148.    {
  149.       if(g)
  150.       {
  151.          if(out_buffer)
  152.          {
  153.             FreeMem(out_buffer,g->outpos);
  154.  
  155.             if(inflated)
  156.             {
  157.                KillSequence(inflated);
  158.                inflated=NULL;
  159.             }
  160.          }
  161.       }
  162.    }
  163.  
  164.    return inflated;
  165. }
  166.  
  167.  
  168.  
  169. /* Huffman code lookup table entry--this entry is four bytes for machines
  170.    that have 16-bit pointers (e.g. PC's in the small or medium model).
  171.    Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
  172.    means that v is a literal, 16 < e < 32 means that v is a pointer to
  173.    the next table, which codes e - 16 bits, and lastly e == 99 indicates
  174.    an unused code.  If a code with e == 99 is looked up, this implies an
  175.    error in the data. */
  176. struct huft {
  177.   UBYTE e;          /* number of extra bits or operation */
  178.   UBYTE b;          /* number of bits in this code or subcode */
  179.   union {
  180.     UWORD n;        /* literal, length base, or distance base */
  181.     struct huft *t; /* pointer to next level of table */
  182.   } v;
  183. };
  184.  
  185. /* Tables for deflate from PKZIP's appnote.txt. */
  186. static ULONG border[] = { /* Order of the bit length code lengths */
  187.   16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  188. static UWORD cplens[] = { /* Copy lengths for literal codes 257..285 */
  189.   3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  190.   35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  191. static UWORD cplext[] = { /* Extra bits for literal codes 257..285 */
  192.   0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  193.   3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
  194. static UWORD cpdist[] = { /* Copy offsets for distance codes 0..29 */
  195.   1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  196.   257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  197.   8193, 12289, 16385, 24577};
  198. static UWORD cpdext[] = { /* Extra bits for distance codes */
  199.   0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  200.   7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
  201.  
  202. /* Macros for inflate() bit peeking and grabbing.
  203.    The usage is:
  204.    
  205.  NEEDBITS(j)
  206.  x = b & mask_bits[j];
  207.  DUMPBITS(j)
  208.  
  209.    where NEEDBITS makes sure that b has at least j bits in it, and
  210.    DUMPBITS removes the bits from b.  The macros use the variable k
  211.    for the number of bits in b.  Normally, b and k are register
  212.    variables for speed, and are initialized at the beginning of a
  213.    routine that uses these macros from a global bit buffer and count.
  214.  
  215.    If we assume that EOB will be the longest code, then we will never
  216.    ask for bits with NEEDBITS that are beyond the end of the stream.
  217.    So, NEEDBITS should not read any more bytes than are needed to
  218.    meet the request.  Then no bytes need to be "returned" to the buffer
  219.    at the end of the last block.
  220.  
  221.    However, this assumption is not true for fixed blocks--the EOB code
  222.    is 7 bits, but the other literal/length codes can be 8 or 9 bits.
  223.    (The EOB code is shorter than other codes because fixed blocks are
  224.    generally short.  So, while a block always has an EOB, many other
  225.    literal/length codes have a significantly lower probability of
  226.    showing up at all.) However, by making the first table have a
  227.    lookup of seven bits, the EOB code will be found in that first
  228.    lookup, and so will not require that too many bits be pulled from
  229.    the stream.
  230.  */
  231.  
  232. UWORD mask_bits[] = {
  233.     0x0000,
  234.     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  235.     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  236. };
  237.  
  238. #define NEXTBYTE()  (g->inbuf[g->inpos++])
  239. #define NEEDBITS(n) {while(k<(n)){b|=((ULONG)NEXTBYTE())<<k;k+=8;}}
  240. #define DUMPBITS(n) {b>>=(n);k-=(n);}
  241.  
  242. /*
  243.    Huffman code decoding is performed using a multi-level table lookup.
  244.    The fastest way to decode is to simply build a lookup table whose
  245.    size is determined by the longest code.  However, the time it takes
  246.    to build this table can also be a factor if the data being decoded
  247.    is not very long.  The most common codes are necessarily the
  248.    shortest codes, so those codes dominate the decoding time, and hence
  249.    the speed.  The idea is you can have a shorter table that decodes the
  250.    shorter, more probable codes, and then point to subsidiary tables for
  251.    the longer codes.  The time it costs to decode the longer codes is
  252.    then traded against the time it takes to make longer tables.
  253.  
  254.    This results of this trade are in the variables lbits and dbits
  255.    below.  lbits is the number of bits the first level table for literal/
  256.    length codes can decode in one step, and dbits is the same thing for
  257.    the distance codes. Subsequent tables are also less than or equal to
  258.    those sizes.  These values may be adjusted either when all of the
  259.    codes are shorter than that, in which case the longest code length in
  260.    bits is used, or when the shortest code is *longer* than the requested
  261.    table size, in which case the length of the shortest code in bits is
  262.    used.
  263.  
  264.    There are two different values for the two tables, since they code a
  265.    different number of possibilities each.  The literal/length table
  266.    codes 286 possible values, or in a flat code, a little over eight
  267.    bits.  The distance table codes 30 possible values, or a little less
  268.    than five bits, flat.  The optimum values for speed end up being
  269.    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  270.    The optimum values may differ though from machine to machine, and
  271.    possibly even between compilers.  Your mileage may vary.
  272.  */
  273.  
  274. #define lbits 9; /* bits in base literal/length lookup table */
  275. #define dbits 6; /* bits in base distance lookup table */
  276.  
  277.  
  278.  
  279. static LONG huft_build(ULONG *b, ULONG n, ULONG s, UWORD *d, UWORD *e, 
  280. struct huft **t, LONG *m, struct GZipData *g)
  281. /* b - code lengths in bits (all assumed <= BMAX) */
  282. /* n - number of codes (assumed <= N_MAX) */
  283. /* s - number of simple-valued codes (0..s-1) */
  284. /* d - list of base values for non-simple codes */
  285. /* e - list of extra bits for non-simple codes */
  286. /* t - result: starting table */
  287. /* m - maximum lookup bits, returns actual */
  288. /* Given a list of code lengths and a maximum table size, make a set of
  289.    tables to decode that set of codes. Return zero on success, one if
  290.    the given code set is incomplete (the tables are still built in this
  291.    case), two if the input is invalid (all zero length codes or an
  292.    oversubscribed set of lengths), and three if not enough memory. */
  293. {
  294.   ULONG a; /* counter for codes of length k */
  295.   ULONG f; /* i repeats in table every f entries */
  296.   LONG g2; /* maximum code length */
  297.   LONG h; /* table level */
  298.   ULONG i; /* counter, current code */
  299.   ULONG j; /* counter */
  300.   LONG k; /* number of bits in current code */
  301.   LONG l; /* bits per table (returned in m) */
  302.   ULONG *p; /* pointer into c[], b[], or v[] */
  303.   struct huft *q; /* points to current table */
  304.   struct huft r; /* table entry for structure assignment */
  305.   LONG w; /* bits before this table == (l * h) */
  306.   ULONG *xp; /* pointer into x */
  307.   LONG y; /* number of dummy codes added */
  308.   ULONG z; /* number of entries in current table */
  309.  
  310.   /* Generate counts for each bit length */
  311.   for(i = 0; i < BMAX+1; ++i)
  312.     g->c[i] = 0;
  313.  
  314.   p = b; i = n;
  315.   do {
  316.     g->c[*p++]++; /* assume all entries <= BMAX */
  317.   } while (--i);
  318.   if (g->c[0] == n) /* null input--all zero length codes */
  319.   {
  320.     *t = (struct huft *)NULL;
  321.     *m = 0;
  322.     return 0;
  323.   }
  324.  
  325.   /* Find minimum and maximum length, bound *m by those */
  326.   l = *m;
  327.   for (j = 1; j <= BMAX; j++)
  328.     if (g->c[j])
  329.       break;
  330.   k = j; /* minimum code length */
  331.   if ((ULONG)l < j)
  332.     l = j;
  333.   for (i = BMAX; i; i--)
  334.     if (g->c[i])
  335.       break;
  336.   g2 = i; /* maximum code length */
  337.   if ((ULONG)l > i)
  338.     l = i;
  339.   *m = l;
  340.  
  341.  
  342.   /* Adjust last length count to fill out codes, if needed */
  343.   for (y = 1 << j; j < i; j++, y <<= 1)
  344.     if((y -= g->c[j]) < 0)
  345.       return 2; /* bad input: more codes than bits */
  346.   if ((y -= g->c[i]) < 0)
  347.     return 2;
  348.   g->c[i] += y;
  349.  
  350.  
  351.   /* Generate starting offsets into the value table for each length */
  352.   g->x[1] = j = 0;
  353.   p = g->c + 1;  xp = g->x + 2;
  354.   while (--i) { /* note that i == g from above */
  355.     *xp++ = (j += *p++);
  356.   }
  357.  
  358.  
  359.   /* Make a table of values in order of bit lengths */
  360.   p = b;  i = 0;
  361.   do {
  362.     if ((j = *p++) != 0)
  363.       g->v[g->x[j]++] = i;
  364.   } while (++i < n);
  365.  
  366.  
  367.   /* Generate the Huffman codes and for each, make the table entries */
  368.   g->x[0] = i = 0; /* first Huffman code is zero */
  369.   p = g->v; /* grab values in bit order */
  370.   h = -1; /* no tables yet--level -1 */
  371.   w = -l; /* bits decoded == (l * h) */
  372.   g->u[0] = (struct huft *)NULL; /* just to keep compilers happy */
  373.   q = (struct huft *)NULL; /* ditto */
  374.   z = 0; /* ditto */
  375.  
  376.   /* go through the bit lengths (k already is bits in shortest code) */
  377.   for (; k <= g2; k++)
  378.   {
  379.     a = g->c[k];
  380.     while (a--)
  381.     {
  382.       /* here i is the Huffman code of length k bits for value *p */
  383.       /* make tables up to required level */
  384.       while (k > w + l)
  385.       {
  386.  h++;
  387.  w += l; /* previous table always l bits */
  388.  
  389.  /* compute minimum size table less than or equal to l bits */
  390.  z = (z = g2 - w) > (ULONG)l ? l : z;  /* upper limit on table size */
  391.  if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
  392.  { /* too few codes for k-w bit table */
  393.   f -= a + 1; /* deduct codes from patterns left */
  394.   xp = g->c + k;
  395.   while (++j < z) /* try smaller tables up to z bits */
  396.   {
  397.     if ((f <<= 1) <= *++xp)
  398.       break; /* enough codes to use up j bits */
  399.     f -= *xp; /* else deduct codes from patterns */
  400.   }
  401. }
  402. z = 1 << j; /* table entries for j-bit table */
  403.  
  404. /* allocate and link in new table */
  405. if(!(q = (struct huft *)AllocVec((z + 1)*sizeof(struct huft), MEMF_CLEAR)))
  406. {
  407.   if(h)
  408.     huft_free(g->u[0],g);
  409.   return 100; /* not enough memory */
  410. }
  411. *t = q + 1; /* link to list for huft_free() */
  412. *(t = &(q->v.t)) = (struct huft *)NULL;
  413. g->u[h] = ++q; /* table starts after link */
  414.  
  415. /* connect to last table, if there is one */
  416. if (h)
  417. {
  418.   g->x[h] = i; /* save pattern for backing up */
  419.   r.b = (UBYTE)l; /* bits to dump before this table */
  420.   r.e = (UBYTE)(16 + j); /* bits in this table */
  421.   r.v.t = q; /* pointer to this table */
  422.   j = i >> (w - l); /* (get around Turbo C bug) */
  423.   g->u[h-1][j] = r; /* connect to last table */
  424. }
  425.       }
  426.  
  427.       /* set up table entry in r */
  428.       r.b = (UBYTE)(k - w);
  429.       if (p >= g->v + n)
  430. r.e = 99; /* out of values--invalid code */
  431.       else if (*p < s)
  432.       {
  433. r.e = (UBYTE)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
  434. r.v.n = (UWORD)(*p); /* simple code is just the value */
  435. p++; /* one compiler does not like *p++ */
  436.       }
  437.       else
  438.       {
  439. r.e = (UBYTE)e[*p - s]; /* non-simple--look up in lists */
  440. r.v.n = d[*p++ - s];
  441.       }
  442.  
  443.       /* fill code-like entries with r */
  444.       f = 1 << (k - w);
  445.       for (j = i >> w; j < z; j += f)
  446. q[j] = r;
  447.  
  448.       /* backwards increment the k-bit code i */
  449.       for (j = 1 << (k - 1); i & j; j >>= 1)
  450. i ^= j;
  451.       i ^= j;
  452.  
  453.       /* backup over finished tables */
  454.       while ((i & ((1 << w) - 1)) != g->x[h])
  455.       {
  456. h--; /* don't need to update q */
  457. w -= l;
  458.       }
  459.     }
  460.   }
  461.  
  462.   /* Return true (1) if we were given an incomplete table */
  463.   return y != 0 && g2 != 1;
  464. }
  465.  
  466.  
  467.  
  468. static VOID huft_free(struct huft *t,struct GZipData *g) /* table to free */
  469. /* Free the allocated tables built by huft_build(), which makes a linked
  470.    list of the tables it made, with the links in a dummy first entry of
  471.    each table. */
  472. {
  473.   struct huft *q;
  474.  
  475.   while(t)
  476.   {
  477.     q = (--t)->v.t;
  478.     FreeVec(t);
  479.     t = q;
  480.   }
  481. }
  482.  
  483.  
  484.  
  485. static LONG inflate_codes(struct huft *tl, struct huft *td, LONG bl,
  486.    LONG bd, struct GZipData *g)
  487. /* tl, td -literal/length and distance decoder tables */
  488. /* bl, bd -number of bits decoded by tl[] and td[] */
  489. /* inflate (decompress) the codes in a deflated (compressed) block.
  490.    Return an error code or zero if it all goes ok. */
  491. {
  492.   ULONG e; /* table entry flag/number of extra bits */
  493.   ULONG n, d; /* length and index for copy */
  494.   struct huft *t; /* pointer to table entry */
  495.   ULONG ml, md;  /* masks for bl and bd bits */
  496.   ULONG b; /* bit buffer */
  497.   ULONG k; /* number of bits in bit buffer */
  498.  
  499.   /* make local copies of globals */
  500.   b = g->bb; /* initialize bit buffer */
  501.   k = g->bk;
  502.  
  503.   /* inflate the coded data */
  504.   ml = mask_bits[bl]; /* precompute masks for speed */
  505.   md = mask_bits[bd];
  506.   for(;;) /* do until end of block */
  507.   {
  508.     NEEDBITS((ULONG)bl)
  509.     if ((e = (t = tl + ((ULONG)b & ml))->e) > 16)
  510.       do {
  511. if (e == 99)
  512.   return 1;
  513. DUMPBITS(t->b)
  514. e -= 16;
  515. NEEDBITS(e)
  516.       } while ((e = (t = t->v.t + ((ULONG)b & mask_bits[e]))->e) > 16);
  517.     DUMPBITS(t->b)
  518.     if(e == 16) /* then it's a literal */
  519.     {
  520.       if(g->outpos >= g->outsize)
  521.         return 300;
  522.       if(g->outbuf)
  523.         g->outbuf[g->outpos] = (UBYTE)t->v.n;
  524.       g->outpos++;
  525.     }
  526.     else /* it's an EOB or a length */
  527.     {
  528.       /* exit if end of block */
  529.       if (e == 15)
  530. break;
  531.  
  532.       /* get length of block to copy */
  533.       NEEDBITS(e)
  534.       n = t->v.n + ((ULONG)b & mask_bits[e]);
  535.       DUMPBITS(e);
  536.  
  537.       /* decode distance of block to copy */
  538.       NEEDBITS((ULONG)bd)
  539.       if ((e = (t = td + ((ULONG)b & md))->e) > 16)
  540. do {
  541.   if (e == 99)
  542.     return 1;
  543.   DUMPBITS(t->b)
  544.   e -= 16;
  545.   NEEDBITS(e)
  546. } while ((e = (t = t->v.t + ((ULONG)b & mask_bits[e]))->e) > 16);
  547.       DUMPBITS(t->b)
  548.       NEEDBITS(e)
  549.       d = g->outpos - t->v.n - ((ULONG)b & mask_bits[e]);
  550.       DUMPBITS(e)
  551.  
  552.       /* do the copy */
  553.       if(g->outpos + n >= g->outsize)
  554.         return 300;
  555.       do
  556.       {
  557.         if(g->outbuf)
  558.           g->outbuf[g->outpos] = g->outbuf[d];
  559.         g->outpos++;
  560.         d++;
  561.       } while(--n);
  562.     }
  563.   }
  564.  
  565.   /* restore the globals from the locals */
  566.   g->bb = b; /* restore global bit buffer */
  567.   g->bk = k;
  568.  
  569.   /* done */
  570.   return 0;
  571. }
  572.  
  573.  
  574.  
  575. static LONG inflate_stored(struct GZipData *g)
  576. /* "decompress" an inflated type 0 (stored) block. */
  577. {
  578.   ULONG n; /* number of bytes in block */
  579.   ULONG b; /* bit buffer */
  580.   ULONG k; /* number of bits in bit buffer */
  581.  
  582.   /* make local copies of globals */
  583.   b = g->bb; /* initialize bit buffer */
  584.   k = g->bk;
  585.  
  586.   /* go to byte boundary */
  587.   n = k & 7;
  588.   DUMPBITS(n);
  589.  
  590.   /* get the length and its complement */
  591.   NEEDBITS(16)
  592.   n = ((ULONG)b & 0xffff);
  593.   DUMPBITS(16)
  594.   NEEDBITS(16)
  595.   if(n != (ULONG)((~b) & 0xffff))
  596.     return 1; /* error in compressed data */
  597.   DUMPBITS(16)
  598.  
  599.   /* read and output the compressed data */
  600.   if(g->outpos + n >= g->outsize)
  601.     return 300;
  602.   while (n--)
  603.   {
  604.     NEEDBITS(8)
  605.     if(g->outbuf)
  606.       g->outbuf[g->outpos] = (UBYTE)b;
  607.     g->outpos++;
  608.     DUMPBITS(8)
  609.   }
  610.  
  611.   /* restore the globals from the locals */
  612.   g->bb = b; /* restore global bit buffer */
  613.   g->bk = k;
  614.   return 0;
  615. }
  616.  
  617.  
  618.  
  619. static LONG inflate_fixed(struct GZipData *g)
  620. /* decompress an inflated type 1 (fixed Huffman codes) block.  We should
  621.    either replace this with a custom decoder, or at least precompute the
  622.    Huffman tables. */
  623. {
  624.   LONG i; /* temporary variable */
  625.   struct huft *tl; /* literal/length code table */
  626.   struct huft *td; /* distance code table */
  627.   LONG bl; /* lookup bits for tl */
  628.   LONG bd; /* lookup bits for td */
  629.   ULONG *l; /* length list for huft_build, we need 288 ULONG's */
  630.  
  631.   l = g->data1;
  632.  
  633.   /* set up literal table */
  634.   for (i = 0; i < 144; i++)
  635.     l[i] = 8;
  636.   for (; i < 256; i++)
  637.     l[i] = 9;
  638.   for (; i < 280; i++)
  639.     l[i] = 7;
  640.   for (; i < 288; i++) /* make a complete, but wrong code set */
  641.     l[i] = 8;
  642.   bl = 7;
  643.   if((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl,g)) != 0)
  644.     return i;
  645.  
  646.   /* set up distance table */
  647.   for (i = 0; i < 30; i++) /* make an incomplete code set */
  648.     l[i] = 5;
  649.   bd = 5;
  650.   if((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd,g)) > 1)
  651.   {
  652.     huft_free(tl,g);
  653.     return i;
  654.   }
  655.  
  656.   /* decompress until an end-of-block code */
  657.   i = inflate_codes(tl, td, bl, bd,g);
  658.  
  659.   /* free the decoding tables, return */
  660.   huft_free(tl,g);
  661.   huft_free(td,g);
  662.   return i;
  663. }
  664.  
  665.  
  666.  
  667. static LONG inflate_dynamic(struct GZipData *g)
  668. /* decompress an inflated type 2 (dynamic Huffman codes) block. */
  669. {
  670.   LONG i; /* temporary variables */
  671.   ULONG j;
  672.   ULONG l; /* last length */
  673.   ULONG m; /* mask for bit lengths table */
  674.   ULONG n; /* number of lengths to get */
  675.   struct huft *tl; /* literal/length code table */
  676.   struct huft *td; /* distance code table */
  677.   LONG bl; /* lookup bits for tl */
  678.   LONG bd; /* lookup bits for td */
  679.   ULONG nb; /* number of bit length codes */
  680.   ULONG nl; /* number of literal/length codes */
  681.   ULONG nd; /* number of distance codes */
  682.   ULONG *ll; /* literal/length and distance code lengths, we need 286+30 ULONG's */
  683.   ULONG b; /* bit buffer */
  684.   ULONG k; /* number of bits in bit buffer */
  685.   LONG err;
  686.  
  687.   ll = g->data1;
  688.  
  689.   /* make local bit buffer */
  690.   b = g->bb;
  691.   k = g->bk;
  692.  
  693.   /* read in table lengths */
  694.   NEEDBITS(5)
  695.   nl = 257 + ((ULONG)b & 0x1f); /* number of literal/length codes */
  696.   DUMPBITS(5)
  697.   NEEDBITS(5)
  698.   nd = 1 + ((ULONG)b & 0x1f); /* number of distance codes */
  699.   DUMPBITS(5)
  700.   NEEDBITS(4)
  701.   nb = 4 + ((ULONG)b & 0xf); /* number of bit length codes */
  702.   DUMPBITS(4)
  703.   if (nl > 286 || nd > 30)
  704.     return 1; /* bad lengths */
  705.  
  706.   /* read in bit-length-code lengths */
  707.   for (j = 0; j < nb; j++)
  708.   {
  709.     NEEDBITS(3)
  710.     ll[border[j]] = (ULONG)b & 7;
  711.     DUMPBITS(3)
  712.   }
  713.   for (; j < 19; j++)
  714.     ll[border[j]] = 0;
  715.  
  716.   /* build decoding table for trees--single level, 7 bit lookup */
  717.   bl = 7;
  718.   if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl,g)) != 0)
  719.   {
  720.     if (i == 1)
  721.       huft_free(tl,g);
  722.     return i; /* incomplete code set */
  723.   }
  724.  
  725.   /* read in literal and distance code lengths */
  726.   n = nl + nd;
  727.   m = mask_bits[bl];
  728.   i = l = 0;
  729.   err = 0;
  730.   while ((ULONG)i < n)
  731.   {
  732.     NEEDBITS((ULONG)bl)
  733.     j = (td = tl + ((ULONG)b & m))->b;
  734.     DUMPBITS(j)
  735.     j = td->v.n;
  736.     if (j < 16) /* length of code in bits (0..15) */
  737.       ll[i++] = l = j; /* save last length in l */
  738.     else if (j == 16) /* repeat last length 3 to 6 times */
  739.     {
  740.       NEEDBITS(2)
  741.       j = 3 + ((ULONG)b & 3);
  742.       DUMPBITS(2)
  743.       if ((ULONG)i + j > n)
  744.       {
  745.         err = 1; break;
  746.       }
  747.       while (j--)
  748. ll[i++] = l;
  749.     }
  750.     else if (j == 17) /* 3 to 10 zero length codes */
  751.     {
  752.       NEEDBITS(3)
  753.       j = 3 + ((ULONG)b & 7);
  754.       DUMPBITS(3)
  755.       if((ULONG)i + j > n)
  756.       {
  757.         err = 1; break;
  758.       }
  759.       while (j--)
  760. ll[i++] = 0;
  761.       l = 0;
  762.     }
  763.     else /* j == 18: 11 to 138 zero length codes */
  764.     {
  765.       NEEDBITS(7)
  766.       j = 11 + ((ULONG)b & 0x7f);
  767.       DUMPBITS(7)
  768.       if((ULONG)i + j > n)
  769.       {
  770.         err = 1; break;
  771.       }
  772.       while (j--)
  773. ll[i++] = 0;
  774.       l = 0;
  775.     }
  776.   }
  777.  
  778.   /* free decoding table for trees */
  779.   huft_free(tl,g);
  780.  
  781.   if(err)
  782.     return err;
  783.  
  784.   /* restore the global bit buffer */
  785.   g->bb = b;
  786.   g->bk = k;
  787.  
  788.   /* build the decoding tables for literal/length and distance codes */
  789.   bl = lbits;
  790.   if((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl,g)) != 0)
  791.   {
  792.     if(i == 1)
  793.       huft_free(tl,g);
  794.     return i; /* incomplete code set */
  795.   }
  796.   bd = dbits;
  797.   if((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd,g)) != 0)
  798.   {
  799.     if(i == 1)
  800.       huft_free(td,g);
  801.     huft_free(tl,g);
  802.     return i; /* incomplete code set */
  803.   }
  804.  
  805.   /* decompress until an end-of-block code */
  806.   err = inflate_codes(tl, td, bl, bd, g);
  807.  
  808.   /* free the decoding tables, return */
  809.   huft_free(tl,g);
  810.   huft_free(td,g);
  811.   return err;
  812. }
  813.  
  814.  
  815.  
  816. static BOOL decrunchZip(struct GZipData *g)
  817. {
  818.    LONG e; /* last block flag */
  819.    LONG t; /* block type */
  820.    ULONG b; /* bit buffer */
  821.    ULONG k; /* number of bits in bit buffer */
  822.  
  823.    /* decompress until the last block */
  824.    do
  825.    {
  826.       b = g->bb; k = g->bk;
  827.       NEEDBITS(1) e = (LONG)b & 1; DUMPBITS(1)
  828.       NEEDBITS(2) t = (LONG)b & 3; DUMPBITS(2)
  829.       g->bb = b; g->bk = k;
  830.  
  831.       /* inflate that block type */
  832.       switch(t)
  833.       {
  834.       case 0: t = inflate_stored(g); break;
  835.       case 1: t = inflate_fixed(g); break;
  836.       case 2: t = inflate_dynamic(g); break;
  837.       }
  838.  
  839.       /* Abort if there was a shortage of memory */
  840.  
  841.       if(t==100)
  842.         return FALSE;
  843.  
  844.    } while(!e);
  845.  
  846.    return TRUE;
  847. }
  848.  
  849.  
  850.  
  851.